Completed
Pull Request — develop (#209)
by Xaver
30s
created

node.js ➔ ... ➔ ips.forEach   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
c 0
b 0
f 0
nc 4
dl 0
loc 11
rs 9.4285
nop 2
1
define(['sorttable', 'snabbdom', 'd3-interpolate', 'moment', 'helper', 'utils/node'],
2
  function (SortTable, V, d3Interpolate, moment, helper, nodef) {
3
    'use strict';
4
    V = V.default;
5
6
7
    function showStatImg(o, d) {
8
      var subst = {};
9
      subst['{NODE_ID}'] = d.node_id;
10
      subst['{NODE_NAME}'] = d.hostname.replace(/[^a-z0-9\-]/ig, '_');
11
      subst['{TIME}'] = d.lastseen.format('DDMMYYYYHmmss');
12
      subst['{LOCALE}'] = _.locale();
13
      return helper.showStat(V, o, subst);
14
    }
15
16
    return function (config, el, router, d, linkScale, nodeDict) {
17
      function nodeLink(node) {
18
        return V.h('a', {
19
          props: {
20
            className: node.is_online ? 'online' : 'offline',
21
            href: router.generateLink({ node: node.node_id })
22
          }, on: {
23
            click: function (e) {
24
              router.fullUrl({ node: node.node_id }, e);
25
            }
26
          }
27
        }, node.hostname);
28
      }
29
30
      function nodeIdLink(nodeId) {
31
        if (nodeDict[nodeId]) {
32
          return nodeLink(nodeDict[nodeId]);
33
        }
34
        return nodeId;
35
      }
36
37
      function showGateway(node) {
38
        var gatewayCols = [
39
          V.h('span', [
40
            nodeIdLink(node.gateway_nexthop),
41
            V.h('br'),
42
            _.t('node.nexthop')
43
          ]),
44
          V.h('span', { props: { className: 'ion-arrow-right-c' } }),
45
          V.h('span', [
46
            nodeIdLink(node.gateway),
47
            V.h('br'),
48
            'IPv4'
49
          ]),
50
          V.h('span', [
51
            nodeIdLink(node.gateway6),
52
            V.h('br'),
53
            'IPv6'
54
          ])
55
        ];
56
57
        return V.h('td', { props: { className: 'gateway' } }, gatewayCols);
58
      }
59
60
      function renderNeighbourRow(n) {
61
        var icons = '';
62
        if (helper.hasLocation(n.node)) {
63
          icons = V.h('span', { props: { className: 'ion-location' } });
64
        }
65
66
        return V.h('tr', [
67
          V.h('td', icons),
68
          V.h('td', nodeLink(n.node)),
69
          V.h('td', n.node.clients),
70
          V.h('td', { style: { color: linkScale((n.link.source_tq + n.link.target_tq) / 2) } }, helper.showTq(n.link.source_tq) + ' - ' + helper.showTq(n.link.target_tq)),
71
          V.h('td', helper.showDistance(n.link))
72
        ]);
73
      }
74
75
      var self = this;
76
      var header = document.createElement('h2');
77
      var table = document.createElement('table');
78
      var images = document.createElement('div');
79
      var neighbours = document.createElement('h3');
80
      var headings = [{
81
        name: ''
82
      }, {
83
        name: 'node.nodes',
84
        sort: function (a, b) {
85
          return a.node.hostname.localeCompare(b.node.hostname);
86
        },
87
        reverse: false
88
      }, {
89
        name: 'node.clients',
90
        class: 'ion-people',
91
        sort: function (a, b) {
92
          return a.node.clients - b.node.clients;
93
        },
94
        reverse: true
95
      }, {
96
        name: 'node.tq',
97
        class: 'ion-connection-bars',
98
        sort: function (a, b) {
99
          return a.link.source_tq - b.link.source_tq;
100
        },
101
        reverse: true
102
      }, {
103
        name: 'node.distance',
104
        class: 'ion-arrow-resize',
105
        sort: function (a, b) {
106
          return (a.link.distance === undefined ? -1 : a.link.distance) -
107
            (b.link.distance === undefined ? -1 : b.link.distance);
108
        },
109
        reverse: true
110
      }];
111
      var tableNeighbour = new SortTable(headings, 1, renderNeighbourRow);
112
113
      el.appendChild(header);
114
      el.appendChild(table);
115
      el.appendChild(neighbours);
116
      el.appendChild(tableNeighbour.el);
117
      el.appendChild(images);
118
119
      self.render = function render() {
120
        V.patch(header, V.h('h2', d.hostname));
121
122
        var children = [];
123
124
        config.nodeAttr.forEach(function (row) {
125
          var field = d[row[1]];
126
          if (nodef['show' + row[1]] !== undefined) {
127
            field = nodef['show' + row[1]](d);
128
          }
129
130
          if (field) {
131
            if (typeof field !== 'object') {
132
              field = V.h('td', field);
133
            }
134
135
            children.push(V.h('tr', [
136
              V.h('th', _.t(row[0])),
137
              field
138
            ]));
139
          }
140
        });
141
142
        children.push(helper.attributeEntry(V, 'node.site', nodef.showSite(d, config)));
143
        children.push(helper.attributeEntry(V, 'node.gateway', showGateway(d)));
144
145
        var elNew = V.h('table', children);
146
        table = V.patch(table, elNew);
147
        table.elm.classList.add('attributes');
148
149
        V.patch(neighbours, V.h('h3', _.t('node.link', d.neighbours.length) + ' (' + d.neighbours.length + ')'));
150
        if (d.neighbours.length > 0) {
151
          tableNeighbour.setData(d.neighbours);
152
          tableNeighbour.el.elm.classList.add('node-links');
153
        }
154
155
        if (config.nodeInfos) {
156
          var img = [];
157
          config.nodeInfos.forEach(function (nodeInfo) {
158
            img.push(V.h('h4', nodeInfo.name));
159
            img.push(showStatImg(nodeInfo, d));
160
          });
161
          images = V.patch(images, V.h('div', img));
162
        }
163
      };
164
165
      self.setData = function setData(data) {
166
        if (data.nodeDict[d.node_id]) {
167
          d = data.nodeDict[d.node_id];
168
        }
169
        self.render();
170
      };
171
      return self;
172
    };
173
  });
174